package org.sky.sys.controller;

import org.sky.cache.JVMCached;
import org.sky.exception.ServiceException;
import org.sky.sys.entity.SysMenu;
import org.sky.sys.entity.SysResource;
import org.sky.sys.service.SysFunService;
import org.sky.sys.util.BspUtils;
import org.sky.sys.util.RequestUtils;
import org.sky.sys.util.TreeStru;
import org.sky.util.JsonUtils;
import org.sky.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author weifx
 * @Title: SysMenuController
 * @ProjectName springboot-platform
 * @Description: TODO{功能管理}
 * @date 2019/3/6 16:04
 * @Version 1.0
 */
@Controller
@RequestMapping(value="/sys/SysFun")
public class SysFunController {
    @Autowired
    private SysFunService sysFunService;

    /**
     * 功能管理页面
     * @return
     */
    @GetMapping(value="/initSysFunMngPage")
    public String initSysFunMngPage(){
        return "sys/fun/sysfunmng";
    }
    /**
     * 功能管理树数据查询
     * @param menuCode
     * @param isLeaf
     * @return
     */
    @PostMapping(value = "/getSysFunctionTree/{menuCode}/{isLeaf}",produces = "application/json;charset=UTF-8")
    public @ResponseBody
    String getSysFunctionTree(
            @PathVariable String menuCode,
            @PathVariable String isLeaf){
        List<TreeStru> list = sysFunService.getFunctionTree(menuCode,isLeaf);
        return JsonUtils.obj2json(list);
    }
    /**
     * 根据主键查询菜单表
     * @param id
     * @return
     */
    @GetMapping(value = "/getSysMenuById/{id}",produces = "application/json;charset=UTF-8")
    public @ResponseBody String getSysMenuById(
            @PathVariable Long id){
        SysMenu bean = sysFunService.getSysMenuById(id);
        return JsonUtils.obj2json(bean);
    }

    /**
     * 根据主键查询资源
     * @param id
     * @return
     */
    @GetMapping(value = "/getSysResourceById/{id}",produces = "application/json;charset=UTF-8")
    public @ResponseBody String getSysResourceById(
            @PathVariable Long id){
        SysResource bean = sysFunService.getSysResourceById(id);
        return JsonUtils.obj2json(bean);
    }
    /**
     * 增加菜单页面
     * @return
     */
    @GetMapping(value="/initSysMenuAddPage")
    public String initSysMenuAddPage(){
        return "sys/fun/sysmenuedit";
    }
    /**
     * 修改菜单页面
     * @return
     */
    @GetMapping(value="/initSysMenuEditPage")
    public String initSysMenuEditPage(){
        return "sys/fun/sysmenuedit";
    }
    /**
     * 增加资源页面
     * @return
     */
    @GetMapping(value="/initSysResourceAddPage")
    public String initSysResourceAddPage(){
        return "sys/fun/sysresourceedit";
    }
    /**
     * 修改资源页面
     * @return
     */
    @GetMapping(value="/initSysResourceEditPage")
    public String initSysResourceEditPage(){
        return "sys/fun/sysresourceedit";
    }

    /**
     * 保存{新增/修改}菜单
     * @param request
     * @return
     */
    @PostMapping(value = "/saveAddEditSysMenu",produces = "application/json;charset=UTF-8")
    public @ResponseBody String saveAddEditSysMenu(HttpServletRequest request){
        SysMenu sysMenu = RequestUtils.getEntityBean(request,SysMenu.class);
        Result rs = null;
        try {
            sysFunService.saveAddEditSysMenu(sysMenu);
            rs = Result.success("保存成功");
        }catch (ServiceException e){
            rs = Result.error("保存失败",e.getMessage());
        }
        return JsonUtils.obj2json(rs);
    }

    /**
     * 保存{新增/修改}资源
     * @param request
     * @return
     */
    @PostMapping(value = "/saveAddEditSysResource",produces = "application/json;charset=UTF-8")
    public @ResponseBody String saveAddEditSysResource(HttpServletRequest request){
        SysResource sysResource = RequestUtils.getEntityBean(request,SysResource.class);
        Result rs = null;
        try {
            sysFunService.saveAddEditSysResource(sysResource);
            rs = Result.success("保存成功");
        }catch (ServiceException e){
            rs = Result.error("保存失败",e.getMessage());
        }
        return JsonUtils.obj2json(rs);
    }

    /**
     * 删除菜单
     * @param request
     * @return
     */
    @PostMapping(value = "/delSysMenu",produces = "application/json;charset=UTF-8")
    public @ResponseBody String delSysMenu(HttpServletRequest request){
        Result result = null;
        try {
            String data = request.getParameter("data");
            Map pm = JsonUtils.json2map(data);
            String code = (String) pm.get("code");
            sysFunService.delSysMenuByCode(code);
            result = Result.success("删除成功");
        }catch (ServiceException e){
            result = Result.error("删除失败",e.getMessage());
        }
        return JsonUtils.obj2json(request);
    }
    @PostMapping(value = "/delSysResource",produces = "application/json;charset=UTF-8")
    public @ResponseBody String delSysResource(HttpServletRequest request){
        Result result = null;
        try {
            String data=request.getParameter("data");
            Map pm = JsonUtils.json2map(data);
            String code = (String)pm.get("code");
            sysFunService.delSysResourceByCode(code);
            result = Result.success("删除成功");
        }catch (ServiceException e){
            result = Result.error("删除失败",e.getMessage());
        }
        return JsonUtils.obj2json(result);
    }

    /**
     * 刷新功能内存
     * @return
     */
    @PostMapping(value = "/refreshSysFunJVMCached",produces = "application/json;charset=UTF-8")
    public @ResponseBody String refreshSysFunJVMCached(){
        Result result = null;
        try {
            BspUtils.getBean(JVMCached.class).refreshSysFunJVMCached();
            result = Result.success("刷新成功");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            result = Result.error("刷新失败",null);
        }
        return JsonUtils.obj2json(result);
    }
}
